package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherInterfaceCreditConfigMapper;
import com.zjdiepu.www.dao.system.BaseSalersmanMapper;
import com.zjdiepu.www.ibiz.other.IOtherInterfaceCreditConfigService;
import com.zjdiepu.www.model.other.dto.OtherInterfaceCreditConfig;
import com.zjdiepu.www.model.other.ext.OtherInterfaceCreditConfigExt;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * other_接口查征信配置Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2018年03月16日 11时40分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherInterfaceCreditConfigServiceImpl extends BaseService implements IOtherInterfaceCreditConfigService {
	public static final Logger logger = LoggerFactory.getLogger(OtherInterfaceCreditConfigServiceImpl.class);

	@Autowired
	private OtherInterfaceCreditConfigMapper otherInterfaceCreditConfigMapper;
	@Autowired
	private BaseSalersmanMapper baseSalersmanMapper;
	
	/**
	 *  接口查询征信配置:根据业务员ID
	 * @author xiongxiaotun <br/>
	 * @Title: getOtherInterfaceCreditConfigBySalesmanId  <br/>
	 * @Description:  接口查询征信配置:根据业务员ID <br/>
	 * @param bankCode 银行编码
	 * @param salesmanId
	 * @return
	 * @version V1.0  <br/>
	 */
	@Override
	public OtherInterfaceCreditConfig getOtherInterfaceCreditConfigBySalesmanId(String bankCode, String salesmanId){
		if(StringUtils.isBlank(salesmanId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY.getCode().toString(), "业务员ID为空");
		}
		BaseSalersman baseSalersman = baseSalersmanMapper.selectByPrimaryKey(salesmanId);
		if(null == baseSalersman) {
			ExceptionHelper.thow(ResultFlagType.MISSING.getCode().toString(), "业务员记录不存在或丢失");
		}
		String unitCode = baseSalersman.getUnitCode();
		return getOtherInterfaceCreditConfigByUnitCode(bankCode, unitCode);
	}
	
	/**
	 * 接口查询征信配置:根据组织编码
	 * @author xiongxiaotun <br/>
	 * @Title: getOtherInterfaceCreditConfigByUnitCode  <br/>
	 * @Description: 接口查询征信配置  <br/>
	 * @param bankCode 银行编码
	 * @param unitCode
	 * @return
	 * @version V1.0  <br/>
	 */
	@Override
	public OtherInterfaceCreditConfig getOtherInterfaceCreditConfigByUnitCode(String bankCode, String unitCode)	{
		if(StringUtils.isBlank(unitCode)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY.getCode().toString(), "业务机构编码丢失");;
		}
		
		if(unitCode.length() < 2) {
			ExceptionHelper.thow(ResultFlagType.PARAM_INVALID.getCode().toString(), "业务机构编码不符合规则");
		}
		
		OtherInterfaceCreditConfig otherInterfaceCreditConfig = new OtherInterfaceCreditConfig();
		otherInterfaceCreditConfig.setDpStatus(DpStatus.NORMAL.getCode());
		otherInterfaceCreditConfig.setBankCode(bankCode);
		otherInterfaceCreditConfig.setBusiUnitCode(unitCode);
		otherInterfaceCreditConfig.setOrderSql("`CREATED` DESC");
		otherInterfaceCreditConfig = otherInterfaceCreditConfigMapper.selectRefined(otherInterfaceCreditConfig);
		if(null == otherInterfaceCreditConfig) {
			if(unitCode.length() < 4) {
				return null;
			} else {
				String finalUnitCode = null;
				if(unitCode.length() == 4) { //4
					finalUnitCode = unitCode.substring(0, unitCode.length() - 2);
				} else { // > 4
					finalUnitCode = unitCode.substring(0, unitCode.length() - 3);
				}
				return getOtherInterfaceCreditConfigByUnitCode(bankCode, finalUnitCode);
			}
		} else {
			return otherInterfaceCreditConfig;
		}
	}
	/**
	 * 检查配置是否重复
	 * @author xiongxiaotun <br/>
	 * @Title checkRepeat  <br/>
	 * @param otherInterfaceCreditConfig
	 * @return
	 * @version V1.0  <br/>
	 */
	private boolean checkRepeat(OtherInterfaceCreditConfig otherInterfaceCreditConfig) {
		OtherInterfaceCreditConfig otherInterfaceCreditConfigParam = new OtherInterfaceCreditConfig();
		otherInterfaceCreditConfigParam.setDpStatus(DpStatus.NORMAL.getCode());
		otherInterfaceCreditConfigParam.setBankCode(otherInterfaceCreditConfig.getBankCode());
		otherInterfaceCreditConfigParam.setBusiUnitCode(otherInterfaceCreditConfig.getBusiUnitCode());
		int count = otherInterfaceCreditConfigMapper.countRefined(otherInterfaceCreditConfigParam);
		if(count == 0) {
			return false;
		} else if(count > 1) {
			return true;
		}
		String id = otherInterfaceCreditConfig.getId();
		if(StringUtils.isBlank(id)) {
			return true;
		}
		otherInterfaceCreditConfigParam = otherInterfaceCreditConfigMapper.selectRefined(otherInterfaceCreditConfigParam);
		if(null == otherInterfaceCreditConfigParam) {
			return false;
		}
		return !otherInterfaceCreditConfigParam.getId().equals(id);
	}
	
	@Override
	public void saveUpdateAndDelete(OtherInterfaceCreditConfig otherInterfaceCreditConfig, OperateType operType) {
		if(OperateType.delete != operType) {
			boolean repeat = checkRepeat(otherInterfaceCreditConfig);
			if(repeat) {
				ExceptionHelper.thow(ResultFlagType.MULTI_INTERFACE_CREDIT_CONFIG);
			}
		}
		switch (operType) {
		case add:
			otherInterfaceCreditConfig.initInsertData(otherInterfaceCreditConfig.getModifier(), otherInterfaceCreditConfig.getModifierName());
			otherInterfaceCreditConfigMapper.insert(otherInterfaceCreditConfig);
			break;
		case edit:
			otherInterfaceCreditConfig.initUpdateData(otherInterfaceCreditConfig.getModifier(), otherInterfaceCreditConfig.getModifierName());
			// 影响的记录数
			int updateCount = otherInterfaceCreditConfigMapper.updateByPrimaryKeySelective(otherInterfaceCreditConfig);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherInterfaceCreditConfig.initDeleteData(otherInterfaceCreditConfig.getModifier(), otherInterfaceCreditConfig.getModifierName());
			// 影响的记录数
			int deleteCount = otherInterfaceCreditConfigMapper.updateByPrimaryKeySelective(otherInterfaceCreditConfig);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherInterfaceCreditConfig> selectByExt(OtherInterfaceCreditConfigExt otherInterfaceCreditConfigExt) {
		return otherInterfaceCreditConfigMapper.selectByExt(otherInterfaceCreditConfigExt);
	}
	
	@Override
	public int countByExt(OtherInterfaceCreditConfigExt otherInterfaceCreditConfigExt){
		return otherInterfaceCreditConfigMapper.countByExt(otherInterfaceCreditConfigExt);
	}
	@Override
	public List<OtherInterfaceCreditConfig> selectMulitlist(OtherInterfaceCreditConfigExt otherInterfaceCreditConfigExt) {
		return otherInterfaceCreditConfigMapper.selectMulitlist(otherInterfaceCreditConfigExt);
	}
	
	@Override
	public int countMulitlist(OtherInterfaceCreditConfigExt otherInterfaceCreditConfigExt){
		return otherInterfaceCreditConfigMapper.countMulitlist(otherInterfaceCreditConfigExt);
	}
	
	@Override
	public OtherInterfaceCreditConfig selectById(String id) {
		return otherInterfaceCreditConfigMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherInterfaceCreditConfigMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherInterfaceCreditConfig otherInterfaceCreditConfig) {
		return otherInterfaceCreditConfigMapper.countRefined(otherInterfaceCreditConfig);
	}

	@Override
	public OtherInterfaceCreditConfig selectRefined(OtherInterfaceCreditConfig otherInterfaceCreditConfig) {
		return otherInterfaceCreditConfigMapper.selectRefined(otherInterfaceCreditConfig);
	}
	
	@Override
	public int deletedRefined(OtherInterfaceCreditConfig otherInterfaceCreditConfig) {
		return otherInterfaceCreditConfigMapper.deletedRefined(otherInterfaceCreditConfig);
	}

	@Override
	public boolean repeat(OtherInterfaceCreditConfig otherInterfaceCreditConfig) {
		int count = countRefined(otherInterfaceCreditConfig);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherInterfaceCreditConfig.getId())) {
			return true;
		}
		OtherInterfaceCreditConfig dbItem = selectRefined(otherInterfaceCreditConfig);
		if(null == dbItem) {
			return false;
		}
		return !otherInterfaceCreditConfig.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherInterfaceCreditConfigMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherInterfaceCreditConfig> otherInterfaceCreditConfigs) {
		return otherInterfaceCreditConfigMapper.insertBatch(otherInterfaceCreditConfigs);
	}
}
